﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace PhoneApp1
{
    public class TowerItem : Item
    {
        /// <summary>
        /// 射击半径
        /// </summary>
        public int Radius { get; set; }

        public TowerItem(GameBase game)
            : base(game)
        {
            this.x = 40;
            this.y = 40;
            this.Type = ItemType.Tower;
            this.width = 20;
            this.height = 20;
            this.Radius = 100;
            this.sd = 2;
            this.EnemyList = new List<Item>();
            Rectangle rect = new Rectangle();
            rect.Width = this.width;
            rect.Height = this.height;
            rect.Fill = new SolidColorBrush(Colors.Blue);
            this.ThisShape = rect;
            base.RegisterEvents();
            this.Game.AddItem(this);
            this.Draw();
            IsCanSj = true;
            this.timer = new Timer(Fire, null, 0, (long)this.sd * 1000);

        }

        private void Fire(object state)
        {
            if (CheckHasEnemy())
            {
                CreateZd();
            }
        }
        public override void Draw()
        {
            base.Draw();
            Canvas.SetLeft(this.ThisShape, this.x - this.width / 2);
            Canvas.SetTop(this.ThisShape, this.y - this.height / 2);
            Canvas.SetZIndex(this.ThisShape, this.z);
        }
        public bool CheckHasEnemy()
        {
            return EnemyList.Count > 0;
        }
        private List<Item> EnemyList;
        private Item CurEnemy;
        /// <summary>
        /// 是否可以射击
        /// </summary>
        private bool IsCanSj;

        public void SetEnemies(List<Item> list)
        {
            lock (EnemyList)
            {
                EnemyList = list;
                CurEnemy = list.FirstOrDefault();
            }
        }

        public bool CheckEnemyInRadius(int x, int y)
        {
            return Math.Pow((x - this.x), 2) + Math.Pow((y - this.y), 2) < Math.Pow(this.Radius, 2);
        }

        public void CreateZd()
        {
            this.CurEnemy = new Item(this.Game);
            this.CurEnemy.x = 40;
            this.CurEnemy.y = 20;
            ZdItem zd = new ZdItem(this.Game, this.x, this.y, CurEnemy.x, CurEnemy.y);
            //zd.Run();
        }
    }
}
